"""
@Project:education
@File:serializers.py
@Author:袁浩天
@Date:11:41
"""

from rest_framework import serializers
from user.models import *
from rest_framework.validators import ValidationError
import re


# 角色
class RoleSerializers(serializers.ModelSerializer):
    class Meta:
        model = RoleModel
        fields = "__all__"


# 用户
class UserSerializers(serializers.ModelSerializer):
    role = serializers.PrimaryKeyRelatedField(queryset=RoleModel.objects.all())
    role_name = serializers.SerializerMethodField()

    school = serializers.PrimaryKeyRelatedField(queryset=SchoolModel.objects.all())
    school_name = serializers.SerializerMethodField()

    def get_role_name(self, obj):
        return obj.role.role_name

    def get_school_name(self, obj):
        return obj.school.school_name

    class Meta:
        model = UserModel
        fields = "__all__"


class UserSerializersTow(serializers.ModelSerializer):

    class Meta:
        model = UserModel
        fields = "__all__"

# 学校
class SchoolSerializers(serializers.ModelSerializer):
    class Meta:
        model = SchoolModel
        fields = "__all__"


# 班级
class TheClassSerializers(serializers.ModelSerializer):
    school = serializers.PrimaryKeyRelatedField(queryset=SchoolModel.objects.all())
    school_name = serializers.SerializerMethodField()

    def get_school_name(self, obj):
        return obj.school.school_name

    class Meta:
        model = SchoolModel
        fields = "__all__"


# 教师
class TeacherSerializers(serializers.ModelSerializer):
    the_class = serializers.PrimaryKeyRelatedField(queryset=SchoolModel.objects.all())
    the_class_name = serializers.SerializerMethodField()

    user = serializers.PrimaryKeyRelatedField(queryset=SchoolModel.objects.all())
    user_name = serializers.SerializerMethodField()

    def get_the_class_name(self, obj):
        return obj.the_class.the_class_name

    def get_user_name(self, obj):
        return obj.user.user_name

    class Meta:
        model = TeacherModel
        fields = "__all__"


# 学生
class StudentsSerializers(serializers.ModelSerializer):
    the_class = serializers.PrimaryKeyRelatedField(queryset=SchoolModel.objects.all())
    the_class_name = serializers.SerializerMethodField()

    user = serializers.PrimaryKeyRelatedField(queryset=SchoolModel.objects.all())
    user_name = serializers.SerializerMethodField()

    def get_the_class_name(self,obj):
        return obj.the_class.the_class_name

    def get_user_name(self,obj):
        return obj.user.user_name

    class Meta:
        model = StudentsModelModel
        fields = "__all__"


# 类型
class TypeSerializers(serializers.ModelSerializer):
    class Meta:
        model = TypeModel
        fields = "__all__"


# 标签
class LabelSerializers(serializers.ModelSerializer):
    class Meta:
        model = LabelModel
        fields = "__all__"


# 学习内容
class LearningContentSerializers(serializers.ModelSerializer):
    class Meta:
        model = LearningContentModel
        fields = "__all__"


class ResourcesContentSerializers(serializers.ModelSerializer):
    class Meta:
        model = ResourcesContentModel
        fields = "__all__"


# 学习任务
class LearningTaskSerializers(serializers.ModelSerializer):
    class Meta:
        model = LearningTaskModel
        fields = "__all__"


class ResourcesTaskSerializers(serializers.ModelSerializer):
    class Meta:
        model = ResourcesTaskModel
        fields = "__all__"


# 学习资源
class LearningResourcesSerializers(serializers.ModelSerializer):
    class Meta:
        model = LearningResourcesModel
        fields = "__all__"


# 收藏
class CollectSerializers(serializers.ModelSerializer):
    class Meta:
        model = CollectModel
        fields = "__all__"


# 点赞
class LikeSerializers(serializers.ModelSerializer):
    class Meta:
        model = LikeModel
        fields = "__all__"


# 消息
class MessageSerializers(serializers.ModelSerializer):
    class Meta:
        model = MessageModelModel
        fields = "__all__"


# 问候语
class GreetingsSerializers(serializers.ModelSerializer):
    class Meta:
        model = Greetings
        fields = "__all__"


# 评论
class ObserverSerializers(serializers.ModelSerializer):
    class Meta:
        model = ObserverModel
        fields = "__all__"


# 菜单序列化器
class MenuSer(serializers.ModelSerializer):
    class Meta:
        model = Menu
        fields = '__all__'

    def validate(self, data):
        menu_name = data.get("menu_name")
        # 验证用户名是否满足要求
        if not re.findall(r"^\w{2,20}$", menu_name):
            raise ValidationError("菜单名或目录名是2到20位数字、字母或下划线")
        return data

















